package com.john.bms.controller.system;

import static com.john.bms.utils.CollectionUtils.map;

import com.github.pagehelper.PageInfo;
import com.john.bms.model.result.RequestResult;
import com.john.bms.model.system.UserVo;
import com.john.bms.model.system.user.SysUser;
import com.john.bms.model.system.user.UserPage;
import com.john.bms.service.system.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author:张家小帅哥
 * @Date:2020/5/6
 * @Version:1.0
 * @Description:TODO
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @RequiresPermissions("user:view")
    @PostMapping("/userPageList")
    public RequestResult userPageList(UserPage userPage) {
        PageInfo<UserVo> pageUserList = userService.getPageUserList(userPage);
        return RequestResult.of(map(pageUserList, "data"));
    }

    @GetMapping("/userInfo")
    public RequestResult userInfo(String id) {
        UserVo userVo = userService.getUserById(id);
        return RequestResult.of(map(userVo, "data"));
    }

    @GetMapping("/updateUserStatus/{id}/{status}")
    public RequestResult updateUserStatus(@PathVariable("id") String id, @PathVariable("status") String status) {
        boolean result = userService.updateUserState(id, Integer.valueOf(status));
        return RequestResult.of(map(result, "data"));
    }

    @PostMapping("/userAdd")
    public RequestResult userAdd(UserVo userVo) throws Exception {
        boolean result = userService.addUser(userVo);
        return RequestResult.of(map(result, "data"));
    }

    @PostMapping("/userUpdate")
    public RequestResult userUpdate(UserVo userVo) {
        boolean b = userService.updateUser(userVo);
        return RequestResult.of(map(b, "data"));
    }

    /**
     * 根据用户id进行逻辑删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/deleteUser")
    public RequestResult deleteUser(String id) {
        if (id == null) {
            try {
                throw new Exception("删除失败！");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        boolean b = userService.logicDelete(id);
        return RequestResult.of(map(b, "data"));
    }

    @PostMapping("/logout")
    public RequestResult logout() {

        return RequestResult.of(map(null, "data"));
    }


}
